package com.example.douyinproject.video;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.douyinproject.BaseFragment;
import com.example.douyinproject.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JzvdStd;

//网络视频播放页面
public class VideoFragment extends BaseFragment {
    String url = "http://m2.qiushibaike.com/article/list/video?page=1";
    RecyclerView rv;
    List<VideoBean.ItemsBean> items; //数据源
    VideoAdapter adapter;
    PagerSnapHelper snapHelper;
    LinearLayoutManager lm;
    int currentPosition;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video, container, false);
        //1、查找控件
        rv = view.findViewById(R.id.video_rv);
        //2、数据源
        items = new ArrayList<>();
        //3、适配器
        adapter = new VideoAdapter(getContext(), items);
        rv.setAdapter(adapter);
        //4、设计布局管理器
        lm = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rv.setLayoutManager(lm);
        //加载网络数据
        connectNetwork(url);
        //辅助在滚动结束时将Item对齐到某个位置
        snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(rv);
        setRvScrollListener();
        return view;
    }

    private void setRvScrollListener() {
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            //设置recyclerView滑动状态的监听
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE: //静止状态
                        autoPlayVideo(recyclerView);
                    break;
                    case RecyclerView.SCROLL_STATE_DRAGGING: //拖动状态
                        JzvdStd.releaseAllVideos();
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING: //滑行状态
                        JzvdStd.releaseAllVideos();
                        break;
                }
            }
        });
    }

    /*自动播放视频*/
    private void autoPlayVideo(RecyclerView recyclerView) {
        View view = snapHelper.findSnapView(lm);
        //当前固定后的item position
        int position = recyclerView.getChildAdapterPosition(view);
        if (currentPosition != position) {
            //当前固定后的position相同，说明是同一个，只不过滑动了一点，然后就释放了
            JzvdStd.releaseAllVideos();
            RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view);
            if (viewHolder != null && viewHolder instanceof VideoAdapter.VH) {
                ((VideoAdapter.VH) viewHolder).jzvdStd.startVideo();
            }
        }
        currentPosition = position;
    }

    /*失去焦点时*/
    @Override
    public void onStop() {
        super.onStop();
        JzvdStd.releaseAllVideos();
    }

    //连接网络成功时会调用的方法
    @Override
    public void onSuccess(String result) {
        VideoBean videoBean = new Gson().fromJson(result, VideoBean.class);
        List<VideoBean.ItemsBean> list = videoBean.getItems(); //网络数据
        items.addAll(list);
        adapter.notifyDataSetChanged();
    }
}